{# created by xupingmao on 2017/04/05 #}

{% extends base %}

{% block body %}

<div class="card">
    {% set title = "随机字符串" %}
    {% include "tools/base_title.html" %}
</div>

<div class="card">
    <div class="col-md-12">
        <label class="label">字符长度</label>
        <input id="length" value="10"/>
    </div>
    <div class="col-md-12">
        <label class="label">生成的数量</label>
        <input id="count" value="1"/>
    </div>
    <div>字符内容</div>
    <div>
        <ul>
            <li><input id="digit" type="checkbox" checked="true" value="1234567890">1234567890</li>
            <li><input id="lowerAlphabet" type="checkbox" checked="true" value="abcdefghijklmnopqrstuvwxyz">a-z</li>
            <li><input id="upperAlphabet" type="checkbox" checked="true" value="ABCDEFGHIJKLMNOPQRSTUVWXYZ">A-Z</li>
            <li><input id="symbol" type="checkbox" value="~!@#$%^&*">~!@#$%^&*</li>
            <li>其他<br/><textarea id="others" style="width:50%;"></textarea></li>
        </ul>
    </div>

    <div class="row">
        <input type="button" onclick="generate()" value="生成"/>
    </div>
</div>

<div class="card">
    <div class="card-title">结果</div>
    <textarea id="out" style="width: 100%; font-family: monospace; " rows=5></textarea>
</div>

<script type="text/javascript">
    function generate() {
        var dict = $("#others").val();
        
        if ($("#digit")[0].checked) {
            dict += $("#digit").val();
        }

        if ($("#lowerAlphabet")[0].checked) {
            dict += $("#lowerAlphabet").val();
        }

        if ($("#upperAlphabet")[0].checked) {
            dict += $("#upperAlphabet").val();
        }

        if ($("#symbol")[0].checked) {
            dict += $("#symbol").val();
        }

        var dictLength = dict.length;


        var length = parseInt($("#length").val());
        var count = parseInt($("#count").val());
        var out = "";
        for (var c = 0; c < count; c++) {        
            for (var i = 0; i < length; i++) {
                var randomIndex = parseInt(Math.random() * dictLength -1);
                if (randomIndex < 0) {
                    randomIndex = 0;
                }
                console.log(dict, randomIndex);
                out += dict[randomIndex];
            }
            out += "\n";
        }
        $("#out").val(out);
    }
</script>

{% end %}